package com.qcxk.yuqian.system.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @author yangpeng
 * 日期操作工具类，如需添加方法，可直接修改这个类
 */
public class DateUtil {
    private static Map<String, String> numMap = null;
    private static Set<String> keySet = null;

    static {
        numMap = new HashMap<>();
        numMap.put("一", "1");
        numMap.put("二", "2");
        numMap.put("三", "3");
        numMap.put("四", "4");
        numMap.put("五", "5");
        numMap.put("六", "6");
        numMap.put("七", "7");
        numMap.put("八", "8");
        numMap.put("九", "9");
        keySet = numMap.keySet();
    }

    private static ThreadLocal<Map<String, SimpleDateFormat>> dfThreadLocal = new ThreadLocal<Map<String, SimpleDateFormat>>() {
        @Override
        protected synchronized Map<String, SimpleDateFormat> initialValue() {
            SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat datetimeNumFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
            Map<String, SimpleDateFormat> map = new HashMap<String, SimpleDateFormat>();
            map.put("datetime", datetimeFormat);
            map.put("date", dateFormat);
            map.put("time", timeFormat);
            map.put("datetimeNum", datetimeNumFormat);
            return map;
        }
    };

    private static SimpleDateFormat getDatetimeDF() {
        return dfThreadLocal.get().get("datetime");
    }

    private static SimpleDateFormat getDatetimeNumDF() {
        return dfThreadLocal.get().get("datetimeNum");
    }

    private static SimpleDateFormat getDateDF() {
        return dfThreadLocal.get().get("date");
    }

    private static SimpleDateFormat getTimeDF() {
        return dfThreadLocal.get().get("time");
    }

    public static Date now() {
        return new Date();
    }

    public static String formatDate(Date date) {
        return getDateDF().format(date);
    }

    /**
     * @param date
     * @return 返回yyyy-MM-dd HH:mm:ss格式的时间字符串
     */
    public static String formatDatetime(Date date) {
        return getDatetimeDF().format(date);
    }

    public static String formatDatetimeNum(Date date) {
        return getDatetimeNumDF().format(date);
    }

    public static String formatTime(Date date) {
        return getTimeDF().format(date);
    }

    public static String currentDate() {
        return formatDate(now());
    }

    public static String currentDatetime() {
        return formatDatetime(now());
    }

    public static String currentDatetimeNum() {
        return formatDatetimeNum(now());
    }

    public static String currentTime() {
        return formatTime(now());
    }

    public static String formatDatetime(Date date, String pattern) {
        SimpleDateFormat patternDF = new SimpleDateFormat(pattern);
        return patternDF.format(date);
    }

    public static Calendar calendar() {
        Calendar cal = Calendar.getInstance(Locale.CHINESE);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        return cal;
    }

    public static long millis() {
        return System.currentTimeMillis();
    }

    public static int month() {
        return calendar().get(Calendar.MONTH) + 1;
    }

    public static int dayOfMonth() {
        return calendar().get(Calendar.DAY_OF_MONTH);
    }

    public static int dayOfWeek() {
        return calendar().get(Calendar.DAY_OF_WEEK);
    }

    public static int dayOfWeekChina() {
        Calendar c = calendar();
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return dayForWeek;
    }

    public static int dayOfYear() {
        return calendar().get(Calendar.DAY_OF_YEAR);
    }

    public static boolean isBefore(Date src, Date dst) {
        return src.before(dst);
    }

    public static boolean isAfter(Date src, Date dst) {
        return src.after(dst);
    }

    public static boolean isEqual(Date date1, Date date2) {
        return date1.compareTo(date2) == 0;
    }

    /**
     * 判断某个日期是否在某个日期范围
     *
     * @param beginDate 日期范围开始
     * @param endDate   日期范围结束
     * @param src       需要判断的日期
     * @return
     */
    public static boolean between(Date beginDate, Date endDate, Date src) {
        return ((beginDate.before(src) && endDate.after(src)) || src.equals(beginDate) || src.equals(endDate));
    }

    /**
     * 获得当前月的最后一天
     * <p>
     * HH:mm:ss为0，毫秒为999
     *
     * @return
     */
    public static Date lastDayOfMonth() {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_MONTH, 0); // M月置零
        cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
        cal.set(Calendar.MINUTE, 0);// m置零
        cal.set(Calendar.SECOND, 0);// s置零
        cal.set(Calendar.MILLISECOND, 0);// S置零
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);// 月份+1
        cal.set(Calendar.MILLISECOND, -1);// 毫秒-1
        return cal.getTime();
    }

    /**
     * 获得当前月的第一天
     * <p>
     * HH:mm:ss SS为零
     *
     * @return
     */
    public static Date firstDayOfMonth() {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_MONTH, 1); // M月置1
        cal.set(Calendar.HOUR_OF_DAY, 0);// H置零
        cal.set(Calendar.MINUTE, 0);// m置零
        cal.set(Calendar.SECOND, 0);// s置零
        cal.set(Calendar.MILLISECOND, 0);// S置零
        return cal.getTime();
    }

    private static Date weekDay(int week) {
        Calendar cal = calendar();
        cal.set(Calendar.DAY_OF_WEEK, week);
        return cal.getTime();
    }

    /**
     * 获得周五日期
     * <p>
     * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date friday() {
        return weekDay(Calendar.FRIDAY);
    }

    /**
     * 获得周六日期
     * <p>
     * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date saturday() {
        return weekDay(Calendar.SATURDAY);
    }

    /**
     * 获得周日日期
     * <p>
     * 注：日历工厂方法{@link #calendar()}设置类每个星期的第一天为Monday，US等每星期第一天为sunday
     *
     * @return
     */
    public static Date sunday() {
        return weekDay(Calendar.SUNDAY);
    }

    /**
     * 将字符串日期时间转换成java.util.Date类型
     * <p>
     * 日期时间格式yyyy-MM-dd HH:mm:ss
     *
     * @param datetime
     * @return
     * @throws ParseException
     */
    public static Date parseDatetime(String datetime) throws ParseException {
        return getDatetimeDF().parse(datetime);
    }

    /**
     * 将字符串日期转换成java.util.Date类型
     * <p>
     * 日期时间格式yyyy-MM-dd
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String date) throws ParseException {
        return getDateDF().parse(date);
    }

    /**
     * 将字符串日期转换成java.util.Date类型
     * <p>
     * 日期时间格式yyyy-MM-dd
     *
     * @param date
     * @param format
     * @return
     * @throws ParseException
     */
    public static Date parseDate(String date, String format)
            throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.parse(date);
    }

    /**
     * 将字符串日期转换成java.util.Date类型
     * <p>
     * 时间格式 HH:mm:ss
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Date parseTime(String time) throws ParseException {
        return getTimeDF().parse(time);
    }

    /**
     * 根据自定义pattern将字符串日期转换成java.util.Date类型
     *
     * @param datetime
     * @param pattern
     * @return
     * @throws ParseException
     */
    public Date parseDatetime(String datetime, String pattern) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.parse(datetime);
    }

    /**
     * @throws
     * @Title: minusDay
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @param day
     * @param: @param format
     * @param: @return
     * @param: @throws ParseException
     * @return: Date
     * @author yokaihei
     * @Date 2015年8月17日 上午11:12:31
     */
    public static Date minusDay(int day) throws ParseException {
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - day);
        Date endDate = getDateDF().parse(getDateDF().format(date.getTime()));
        return endDate;
    }

    /**
     * 计算当前周的第num周的第一天日期和最后一天日期
     *
     * @param num 当前周的第几周，可以是负数，负数即为前几周
     * @return
     */
    public static Map<String, Date> getWeekStartEndDate(Integer num) {
        Map<String, Date> result = new HashMap<String, Date>();
        Date nowTime = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(nowTime);
        ca.add(Calendar.DATE, 6 * num);
        ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // 当前周第一天
        Date startDate = ca.getTime();
        result.put("startDate", startDate);
        // 当前周最后一天
        ca.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        ca.add(Calendar.WEEK_OF_YEAR, 1);
        Date endDate = ca.getTime();
        result.put("endDate", endDate);
        return result;
    }

    public static String firstDayOfYear() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_YEAR, 1);//本年第一天
        return DateUtil.transferLongToDate("yyyyMMdd", c.getTimeInMillis());
    }

    public static String lastDayOfYear() {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        currCal.set(Calendar.YEAR, currentYear);
        currCal.roll(Calendar.DAY_OF_YEAR, 1);
        return DateUtil.transferLongToDate("yyyyMMdd", currCal.getTimeInMillis());
    }

    public static void main(String[] args) {
        Date date = new Date();
        int days = DateUtil.getDays(date.getYear() + 1900, date.getMonth());
        System.out.println(days);
    }

    /**
     * 计算当前月的第num月的第一天日期和最后一天日期
     *
     * @param num 当前月的第几个月，可以是负数，负数即为前几个月
     * @return
     */
    public static Map<String, Date> getMonthStartEndDate(Integer num) {
        Map<String, Date> result = new HashMap<String, Date>();
        Date nowTime = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(nowTime);
        ca.add(Calendar.MONTH, num);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        Date startDate = ca.getTime();
        ca.set(Calendar.DAY_OF_MONTH,
                ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date endDate = ca.getTime();
        result.put("startDate", startDate);
        result.put("endDate", endDate);
        return result;
    }

    /**
     * @throws
     * @Title: minusDay
     * @Description: (这里用一句话描述这个方法的作用)
     * @param: @param date
     * @param: @param day
     * @param: @param format
     * @param: @return
     * @param: @throws ParseException
     * @return: Date
     * @author yokaihei
     * @Date 2015年8月17日 上午11:12:58
     */
    public static Date minusDay(Date date, int day) throws ParseException {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = date;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(beginDate);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - day);
        Date endDate = dft.parse(dft.format(calendar.getTime()));
        return endDate;
    }

    /**
     * 把毫秒转化成日期
     *
     * @param dateFormat (日期格式，例如：MM/ dd/yyyy HH:mm:ss)
     * @param millSec    (毫秒数)
     * @return
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }

    /**
     * 把日期转化成字符串日期
     *
     * @param dateFormat (日期格式，例如：MM/ dd/yyyy HH:mm:ss)
     * @param date       日期
     * @return
     */
    public static String transferDateToString(String dateFormat, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(date);
    }

    /**
     * 功能：当前时间增加天数。
     *
     * @param days 正值时时间延后，负值时时间提前。
     * @return Date
     */
    public static Date addDays(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
        return new Date(c.getTimeInMillis());
    }

    /**
     * @throws
     * @Title: differDays
     * @Description: (计算两个日期相差天数)
     * @param: @param start
     * @param: @param end
     * @param: @return
     * @return: int
     * @author Joshua_Cheng@qq.com
     * @Date 2015年9月23日 下午8:16:25
     */
    public static int differDays(Date start, Date end) {
        long diff = end.getTime() - start.getTime();
        return (int) (diff / (24 * 60 * 60 * 1000));
    }

    //本年
    public static String getNowYear() {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = new GregorianCalendar();
        c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_YEAR, 1);//本年第一天
        result = result + sdf.format(c.getTime());
        int year = c.get(Calendar.YEAR);
        c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, year);
        c.roll(Calendar.DAY_OF_YEAR, -1);
        result = result + sdf.format(c.getTime());
        return result;
    }

    //本月
    public static String getNowMonth() {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = new GregorianCalendar();
        c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);//本月第一天
        result = result + sdf.format(c.getTime());
        c.add(Calendar.MONTH, 1);//本月最后一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        result = result + sdf.format(c.getTime());
        return result;
    }

    //本周
    public static String getNowWeek() {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        result = result + sdf.format(c.getTime());
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        result = result + sdf.format(c.getTime());
        return result;
    }
    /**
     * ===========查询本天、本周、本月公共方法==============
     * @return
     */
    /**
     * 获取当天0点
     *
     * @return
     */
    public static Date getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date time = cal.getTime();
        return time;
    }

    public static Date getTimesmorning(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date time = cal.getTime();
        return time;
    }

    /**
     * 获得当天24点时间
     *
     * @return
     */
    public static Date getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 59);
        Date time = cal.getTime();
        return time;
    }

    public static Date getTimesnight(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 59);
        Date time = cal.getTime();
        return time;
    }

    //获得本周一0点时间
    public static Date getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_YEAR, -7);
        Date time = cal.getTime();
        return time;
    }

    //获得本月第一天0点时间
    public static Date getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DAY_OF_YEAR, -30);
        Date time = cal.getTime();
        return time;
    }
    /**
     * ===========查询本天、本周、本月公共方法==============
     */


    /**
     * 判断两个日期是否是同一天
     *
     * @param date1 date1
     * @param date2 date2
     * @return
     */
    @SuppressWarnings("unused")
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * @param str
     * @return
     * @desc 判断日期格式是否为yyyy-MM-dd
     * @author xielinqi
     * @date 2016年8月2日 上午11:53:15
     */
    public static boolean isDate(String str) {
        boolean flag = false;
        if (str == null)
            return flag;
        String eL = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
        Pattern p = Pattern.compile(eL);
        Matcher m = p.matcher(str);
        if (m.matches()) {
            String[] ymd = str.split("-");
            if (!ymd[0].startsWith("0") && Integer.parseInt(ymd[1]) > 0 && Integer.parseInt(ymd[1]) <= 12
                    && Integer.parseInt(ymd[2]) > 0 && Integer.parseInt(ymd[2]) <= 31) {
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 判断指定格式的日期是否是有效日期
     *
     * @param str
     * @param pattern
     * @return
     */
    public static boolean isDateNew(String str, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            df.setLenient(false);
            df.parse(str);
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    public static String chineseNumber2Int(String chineseNumber) {
        try {
            if (keySet.contains(chineseNumber)) {
                chineseNumber = numMap.get(chineseNumber);
            }
        } catch (Exception e) {
            return chineseNumber;
        }
        return chineseNumber;
    }

    public static String getDateStr(String question) {
        String result = null;
        try {
            if (question.contains("星期几") || question.contains("周几") || question.contains("多少号") || question.contains("几号") || question.contains("日期")) {
                int num = 0;
                String info = "今天";
                if (question.contains("昨天")) {
                    num = -1;
                    info = "昨天";
                }
                if (question.contains("今天")) {
                    num = 0;
                    info = "今天";
                }
                if (question.contains("明天")) {
                    num = 1;
                    info = "明天";
                }
                if (question.contains("后天")) {
                    num = 2;
                    info = "后天";
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + num);
                Date date = calendar.getTime();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                result = info + getWeekOfDate(date) + "，" + sdf.format(date);
            }
            if (question.contains("几点")) {
                Calendar now = Calendar.getInstance();
                System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
                System.out.println("分: " + now.get(Calendar.MINUTE));
                System.out.println("秒: " + now.get(Calendar.SECOND));
                result = "当前时间为" + now.get(Calendar.HOUR_OF_DAY) + "点" + now.get(Calendar.MINUTE) + "分" + now.get(Calendar.SECOND) + "秒";
            }
        } catch (Exception e) {
        }
        return result;
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }

    public static String getDateFormt(String str) {
        String result = "";
        str = getDateFromStr(str);
        if (StringUtils.isNotEmpty(str) && str.equals("1")) {
            return "";
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String nowDateStr = sdf1.format(new Date());
        String[] strs = nowDateStr.split("-");
        String year = strs[0];
        String month = strs[1];
        String[] ss = null;
        if (StringUtils.isNotEmpty(str)) {
            str = str.replace("年", "-");
            str = str.replace("月", "-");
            str = str.replace("日", "");
            str = str.replace("号", "");
            if (str.contains("/")) {
                ss = str.split("/");
                if (ss.length == 3) {
                    result = str.replaceAll("/", "-");
                } else {
                    result = year + "-" + str.replaceAll("/", "-");
                }
            } else if (str.contains("-")) {
                ss = str.split("-");
                if (ss.length == 3) {
                    result = str;
                } else {
                    result = year + "-" + str;
                }
//    		}else if(str.contains("\\")){
//    			ss = str.split("\\");
//    			if(ss.length == 3){
//    				result = str.replaceAll("\\", "-");
//    			}else{
//    				result = year + "-" + str.replaceAll("\\", "-");
//    			}
            } else if (str.contains(".")) {
                ss = str.split("\\.");
                if (ss.length == 3) {
                    result = str.replaceAll("\\.", "-");
                } else {
                    result = year + "-" + str.replaceAll(".", "-");
                }
            } else {
                result = year + "-" + month + "-" + str;
            }
        }
        String[] rs = result.split("-");
        if (rs != null && rs.length == 3) {
            String months = rs[1];
            String day = rs[2];
            if (day.equals("1") || day.equals("2") || day.equals("3") || day.equals("4") || day.equals("5")
                    || day.equals("6") || day.equals("7") || day.equals("8") || day.equals("9")) {
                day = "0" + day;
            }
            if (months.equals("1") || months.equals("2") || months.equals("3") || months.equals("4") || months.equals("5")
                    || months.equals("6") || months.equals("7") || months.equals("8") || months.equals("9")) {
                months = "0" + months;
            }
            result = rs[0] + "-" + months + "-" + day;
        }
        return result;
    }

    //从字符串中提取出日期
    public static String getDateFromStr(String str) {
        if (str.contains("二十号")) {
            str = str.replace("二十号", "20号");
        }
        if (str.contains("二十日")) {
            str = str.replace("二十日", "20号");
        }
        if (str.contains("三十号")) {
            str = str.replace("三十号", "30号");
        }
        if (str.contains("三十日")) {
            str = str.replace("三十日", "30号");
        }
        if (str.contains("十号")) {
            str = str.replace("十号", "10号");
        }
        if (str.contains("十日")) {
            str = str.replace("十日", "10号");
        }
        if (str.contains("十月")) {
            str = str.replace("十月", "10月");
        }
        str = str.replace("三十", "3");
        str = str.replace("二十", "2");
        str = str.replace("一十", "1");
        str = str.replace("十", "1");
        str = str.replace("一", "1");
        str = str.replace("二", "2");
        str = str.replace("三", "3");
        str = str.replace("四", "4");
        str = str.replace("五", "5");
        str = str.replace("六", "6");
        str = str.replace("七", "7");
        str = str.replace("八", "8");
        str = str.replace("九", "9");
        str = str.replace("零", "0");
        String dateStr = "";
        List<String> matches = null;
        Pattern pattern = Pattern.compile("((\\d{1,4}[-|\\/|年|\\.])?(\\d{1,2}[-|\\/|月|\\.])?\\d{1,2}([日|号])"
                + "?(\\s)*(\\d{1,2}([点|时])?((:)?\\d{1,2}(分)?((:)?\\d{1,2}(秒)?)?)?)?(\\s)*(PM|AM)?)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find() && matcher.groupCount() >= 1) {
            matches = new ArrayList<>();
            for (int i = 1; i <= matcher.groupCount(); i++) {
                String temp = matcher.group(i);
                matches.add(temp);
            }
        } else {
            matches = Collections.EMPTY_LIST;
        }
        if (matches.size() > 0) {
            return ((String) matches.get(0)).trim();
        } else {
        }
        return dateStr;
    }

    public static String getYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * @param year  date.getYear()
     * @param month date.getMonth()
     * @return 2019年7月5日    wangyong
     */
    public static int getDays(int year, int month) {
        int days;
        int FebDay = 28;
        if (isLeap(year))
            FebDay = 29;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = FebDay;
                break;
            default:
                days = 0;
                break;
        }
        return days;
    }

    //判断闰年
    static boolean isLeap(int year) {
        if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))
            return true;
        else
            return false;
    }
}
